import qs from 'qs'
import axios from 'axios'
import router from "../router/index"

let return_token=e=>localStorage.token?{ 'Authorization':'Bearer '+localStorage.token}:{};
let no_token=res=>{
    if(res.code==700){
        localStorage.removeItem('token');
        router.push('/')
    }
};

export const GET = req => {
	let headers =return_token();
	return axios.get(req.url, {
		params: req.data,
		headers
	}).then(res => {
        no_token(res.data);
		return Promise.resolve(res.data)
	}).catch(err=>{
        no_token(err.response.data);
        return Promise.resolve(err.response.data)
    })
};

export const POST = req => {
    let headers =return_token();
	return axios.post(req.url, req.data, {
		transformRequest: data => qs.stringify(data),
		headers
	}).then((res) => {
        no_token(res.data);
		return Promise.resolve(res.data)
	}).catch(err=>{
        no_token(err.response.data);
        return Promise.resolve(err.response.data)
    })
};
